package com.example.administrator.szb.fragments.fragment_forTab.home.adapter;

import android.content.Context;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.fragments.fragment_forTab.home.bean.Supeis;

import java.util.List;
import java.util.Map;

/**
 * Created by yarolegovich on 07.03.2017.
 */

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.MyViewHolder> {
    private Context context;
    private List<Supeis> data;

    public ShopAdapter(List data, Context ctx) {
        this.data = data;
        context=ctx;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View v = inflater.inflate(R.layout.item_supei_card, parent, false);
        return new MyViewHolder(v);
    }

    private int select = 0;



    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener!=null){
                    onItemClickListener.onItemClick(position);
                }
            }
        });
        int n = data.get(position).getFirst().size();
        holder.change(n);
        String timesarr[] = data.get(position).getEnd_time().split(" ");
        holder.tv_time.setText("项目结束时间:"+timesarr[0]);
        holder.title.setText("" + data.get(position).getTitle());
        holder.content.setText(data.get(position).getOther_require());
        List<Map<String, String>> l = data.get(position).getFirst();
        for (int j = 0; j < l.size(); j++) {
            Map<String, String> map = l.get(j);
            for (Map.Entry<String, String> entry : map.entrySet()) {
                if (j==0){
                    holder.tv_value_first.setText(entry.getValue());
                    holder.tv_key_first.setText(entry.getKey());
                }else if (j==1){
                    holder.tv_value_two.setText(entry.getValue());
                    holder.tv_key_two.setText(entry.getKey());
                }
            }

        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        CardView cardView ;
        TextView title,content,tv_time;
        LinearLayout ll_content;
        LinearLayout ll_first;
        TextView tv_value_first;
        TextView tv_key_first;
        LinearLayout ll_two;
        TextView tv_value_two;
        TextView tv_key_two;
        public MyViewHolder(View view)
        {
            super(view);
            tv_time = view.findViewById(R.id.tv_time);
            cardView = view.findViewById(R.id.cardView);
            title = view.findViewById(R.id.title);
            content = view.findViewById(R.id.content);
            ll_content = (LinearLayout) view.findViewById(R.id.ll_content);
            ll_first = (LinearLayout) view.findViewById(R.id.ll_first);
            tv_value_first = (TextView) view.findViewById(R.id.tv_value_first);
            tv_key_first = (TextView) view.findViewById(R.id.tv_key_first);
            ll_two = (LinearLayout) view.findViewById(R.id.ll_two);
            tv_value_two = (TextView) view.findViewById(R.id.tv_value_two);
            tv_key_two = (TextView) view.findViewById(R.id.tv_key_two);
        }

        public void change(int size){
            ll_two.setVisibility(View.GONE);
            content.setVisibility(View.GONE);
            ll_content.setVisibility(View.GONE);
            ll_first.setVisibility(View.GONE);

            if (size>0){
                ll_content.setVisibility(View.VISIBLE);
                ll_first.setVisibility(View.VISIBLE);
                if (size>1){
                    ll_two.setVisibility(View.VISIBLE);
                }
            }else {
                content.setVisibility(View.VISIBLE);
            }
        }

    }

    OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(int pos);
    }
}
